/**
 * 
 */
package co.fitstart.coach.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.utils.DateUtils;

import co.fitstart.coach.dao.CourseDao;
import co.fitstart.coach.dao.ScheduleDaoSupport;
import co.fitstart.coach.service.CourseService;
import co.fitstart.entity.course.CourseType;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.round.RoundStatus;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.course.schedule.ScheduleStatus;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;
import co.fitstart.entity.user.Student;

/**
 * @author downpour
 *
 */
@Service("courseService")
public class CourseServiceImpl implements CourseService {
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Dynamic
    private CourseDao courseDao;
    
    @Autowired
    private ScheduleDaoSupport scheduleDaoSupport;
    
    /* (non-Javadoc)
     * @see co.fitstart.coach.service.CourseService#loadCourseSchedule(java.lang.Long)
     */
    public CourseSchedule loadCourseSchedule(Long id) {
        return genericDaoSupport.load(CourseSchedule.class, id);
    }
    
    /* (non-Javadoc)
     * @see co.fitstart.coach.service.CourseService#getWeeklyCourseSchedule(co.fitstart.common.entity.system.Principal, java.util.Date)
     */
    public List<CourseSchedule> getWeeklyCourseSchedule(Principal principal, Date startDate) {
        
        List<ScheduleStatus> status = Arrays.asList(new ScheduleStatus[] { ScheduleStatus.READY, ScheduleStatus.EXPIRY, ScheduleStatus.FINISH });
        return courseDao.listWeeklyCourseSchedule(principal, startDate, DateUtils.addDays(startDate, 6), status);
    }

    /* (non-Javadoc)
     * @see co.fitstart.coach.service.CourseService#getCourseSchedule(co.fitstart.common.entity.system.Principal, java.util.Date, java.lang.String)
     */
    public List<CourseSchedule> getCourseSchedule(Principal principal, Date day, String hour) {
        
        return courseDao.listCourseSchedule(principal, day, hour);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.CourseService#loadCourseRound(java.lang.Long)
     */
    public CourseRound loadCourseRound(Long courseRoundId) {
        return genericDaoSupport.load(CourseRound.class, courseRoundId);
    }
    
    /* (non-Javadoc)
     * @see co.fitstart.coach.service.CourseService#listCourseRound(co.fitstart.entity.user.Student, co.fitstart.entity.course.CourseType)
     */
    public List<CourseRound> listCourseRound(Student student, CourseType courseType) {
        List<CourseRound> courseRounds = new ArrayList<>();
        
        courseRounds.addAll(courseDao.listCourseRound(student, courseType, RoundStatus.READY));
        courseRounds.addAll(courseDao.listCourseRound(student, courseType, Arrays.asList(new RoundStatus[]{RoundStatus.PREPARE, RoundStatus.ACTIVATE, RoundStatus.EXPIRE, RoundStatus.HOLD})));
        courseRounds.addAll(courseDao.listCourseRound(student, courseType, RoundStatus.FINISH));
        
        return courseRounds;
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.CourseService#listAvaliableTimes(co.fitstart.entity.principal.Principal, co.fitstart.entity.site.Site, java.util.Date)
     */
    public List<String> listAvaliableTimes(Principal coach, Site site, Date date) {
        return scheduleDaoSupport.listAvaliable(coach, site, date);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.CourseService#countFinishedSchedule(co.fitstart.entity.course.round.CourseRound)
     */
    public int countFinishedSchedule(CourseRound courseRound) {
        return courseDao.countFinishedSchedule(courseRound, ScheduleStatus.FINISH);
    }
}
